
//

#import "TCRootViewController.h"
#import "TCFirstViewController.h"
#import "TCMeViewController.h"
#import "TCNewsViewController.h"
#import "TCProductViewController.h"
#import "TCReportViewController.h"
#import "TCConfiguration.h"
#import "UIColor+ICT.h"
#import "TCLoginViewController.h"

typedef NS_ENUM(NSInteger, YWKHomeVCIndex)
{
    YWKHomeVCIndexMonitor = 0,
    YWKHomeVCIndexDoctor,
    YWKHomeVCIndexDiseaseManagement,
    YWKHomeVCIndexMyself,
    
    YWKHomeVCIndexMax
};

NSString * const kShowLoginNotificateion = @"ShowLoginNotificateion ";
@interface TCRootViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) TCFirstViewController *firstHomeVC;
@property (nonatomic, strong) TCProductViewController *productHomeVC;
@property (nonatomic, strong) TCNewsViewController *newsHomeVC;
@property (nonatomic, strong) TCMeViewController *myselfHomeVC;
@property (nonatomic, strong) TCLoginViewController *loginVC;

@end

@implementation TCRootViewController

#pragma mark - life cycle

- (instancetype)init
{
    if (self = [super init])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(presentLoginViewController)
                                                     name:kShowLoginNotificateion
                                                   object:nil];

        //去除导航栏下方黑线
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
        self.tabBar.tintColor = kAPPThemeColor;
    }
    
    return self;
}

- (void)loadView
{
    [super loadView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.tintColor = kYWKNavBarTitleColor;
    //monitor
    UITabBarItem *firstHomeItem = [[UITabBarItem alloc]
                                 initWithTitle:NSLocalizedString(@"首页", @"monitor tab title")
                                                              image:[UIImage imageNamed:@"tabbar_monitor"]
                                                                tag:YWKHomeVCIndexMonitor];
    firstHomeItem.selectedImage = [UIImage imageNamed:@"tabbar_monitor_selected"];
    self.firstHomeVC = [[TCFirstViewController alloc] init];
    UINavigationController * firstHomeNav = [[UINavigationController alloc]
                                               initWithRootViewController:self.firstHomeVC];
    firstHomeNav.tabBarItem = firstHomeItem;
    
    UITabBarItem *productItem = [[UITabBarItem alloc]
                                initWithTitle:NSLocalizedString(@"产品", @"doctor tab title")
                                                             image:[UIImage imageNamed:@"tabbar_doctor"]
                                                               tag:YWKHomeVCIndexDoctor];
    productItem.selectedImage = [UIImage imageNamed:@"tabbar_doctor_selected"];
    self.productHomeVC = [[TCProductViewController alloc] init];
    UINavigationController * productHomeNav = [[UINavigationController alloc]
                                              initWithRootViewController:self.productHomeVC];
    productHomeNav.tabBarItem = productItem;
    
    //
    UITabBarItem *reportItem = [[UITabBarItem alloc]
                                    initWithTitle:NSLocalizedString(@"报告", @"diseaseMgm tab title")
                                                                 image:[UIImage imageNamed:@"tabbar_diseaseMgm"]
                                                                   tag:YWKHomeVCIndexDiseaseManagement];
    self.newsHomeVC = [[TCNewsViewController alloc] init];
    UINavigationController * newsHomeNav = [[UINavigationController alloc]
                                                  initWithRootViewController:self.newsHomeVC];
    newsHomeNav.tabBarItem = reportItem;
    
    //myself
    UITabBarItem *myselfItem = [[UITabBarItem alloc]
                                initWithTitle:NSLocalizedString(@"我的", @"myself tab title")
                                                             image:[UIImage imageNamed:@"tabbar_myself"]
                                                               tag:YWKHomeVCIndexMyself];
    myselfItem.selectedImage = [UIImage imageNamed:@"tabbar_myself_selected"];
    self.myselfHomeVC = [[TCMeViewController alloc] init];
    UINavigationController * myselfHomeNav = [[UINavigationController alloc]
                                              initWithRootViewController:self.myselfHomeVC];
    myselfHomeNav.tabBarItem = myselfItem;
    
    self.viewControllers = @[firstHomeNav, productHomeNav, newsHomeNav, myselfHomeNav];
    
    [self showGuidViews];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate=self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    //[self presentLoginViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kShowLoginNotificateion object:nil];
}

#pragma mark - event response


#pragma mark - tabBarDelegate
//禁止tab多次点击
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    UIViewController *tbselect=tabBarController.selectedViewController;
    if([tbselect isEqual:viewController]){
        return NO;
    }
    return YES;
}


#pragma mark - private methods

- (void)presentLoginViewController
{
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:self.loginVC];
    nav.navigationBarHidden = YES;
    
    [self presentViewController:nav animated:YES completion:nil];
}

/**
 *  显示引导页
 */
- (void)showGuidViews
{
    
//     
//        //显示引导页

}

#pragma mark - getters and setters

- (TCLoginViewController *)loginVC
{
    if (!_loginVC)
    {
        _loginVC = [[TCLoginViewController alloc] init];
    }
//    [_loginVC clearUserAccont];// 清理 账号
    return _loginVC;
}



@end
